home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cpp.info-1 (.txt) < prev    next >
GNU Info File  |  1994-07-13  |  49KB  |  908 lines

  1. This is Info file cpp.info, produced by Makeinfo-1.54 from the input
  2. file cpp.texi.
  3.    This file documents the GNU C Preprocessor.
  4.    Copyright 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the entire resulting derived work is distributed under the terms
  11. of a permission notice identical to this one.
  12.    Permission is granted to copy and distribute translations of this
  13. manual into another language, under the above conditions for modified
  14. versions.
  15. File: cpp.info,  Node: Top,  Next: Global Actions,  Up: (DIR)
  16. The C Preprocessor
  17. ******************
  18.    The C preprocessor is a "macro processor" that is used automatically
  19. by the C compiler to transform your program before actual compilation.
  20. It is called a macro processor because it allows you to define "macros",
  21. which are brief abbreviations for longer constructs.
  22.    The C preprocessor provides four separate facilities that you can
  23. use as you see fit:
  24.    * Inclusion of header files.  These are files of declarations that
  25.      can be substituted into your program.
  26.    * Macro expansion.  You can define "macros", which are abbreviations
  27.      for arbitrary fragments of C code, and then the C preprocessor will
  28.      replace the macros with their definitions throughout the program.
  29.    * Conditional compilation.  Using special preprocessor commands, you
  30.      can include or exclude parts of the program according to various
  31.      conditions.
  32.    * Line control.  If you use a program to combine or rearrange source
  33.      files into an intermediate file which is then compiled, you can
  34.      use line control to inform the compiler of where each source line
  35.      originally came from.
  36.    C preprocessors vary in some details.  This manual discusses the GNU
  37. C preprocessor, the C Compatible Compiler Preprocessor.  The GNU C
  38. preprocessor provides a superset of the features of ANSI Standard C.
  39.    ANSI Standard C requires the rejection of many harmless constructs
  40. commonly used by today's C programs.  Such incompatibility would be
  41. inconvenient for users, so the GNU C preprocessor is configured to
  42. accept these constructs by default.  Strictly speaking, to get ANSI
  43. Standard C, you must use the options `-trigraphs', `-undef' and
  44. `-pedantic', but in practice the consequences of having strict ANSI
  45. Standard C make it undesirable to do this.  *Note Invocation::.
  46. * Menu:
  47. * Global Actions::    Actions made uniformly on all input files.
  48. * Commands::          General syntax of preprocessor commands.
  49. * Header Files::      How and why to use header files.
  50. * Macros::            How and why to use macros.
  51. * Conditionals::      How and why to use conditionals.
  52. * Combining Sources:: Use of line control when you combine source files.
  53. * Other Commands::    Miscellaneous preprocessor commands.
  54. * Output::            Format of output from the C preprocessor.
  55. * Invocation::        How to invoke the preprocessor; command options.
  56. * Concept Index::     Index of concepts and terms.
  57. * Index::             Index of commands, predefined macros and options.
  58. File: cpp.info,  Node: Global Actions,  Next: Commands,  Prev: Top,  Up: Top
  59. Transformations Made Globally
  60. =============================
  61.    Most C preprocessor features are inactive unless you give specific
  62. commands to request their use.  (Preprocessor commands are lines
  63. starting with `#'; *note Commands::.).  But there are three
  64. transformations that the preprocessor always makes on all the input it
  65. receives, even in the absence of commands.
  66.    * All C comments are replaced with single spaces.
  67.    * Backslash-Newline sequences are deleted, no matter where.  This
  68.      feature allows you to break long lines for cosmetic purposes
  69.      without changing their meaning.
  70.    * Predefined macro names are replaced with their expansions (*note
  71.      Predefined::.).
  72.    The first two transformations are done *before* nearly all other
  73. parsing and before preprocessor commands are recognized.  Thus, for
  74. example, you can split a line cosmetically with Backslash-Newline
  75. anywhere (except when trigraphs are in use; see below).
  76.      /*
  77.      */ # /*
  78.      */ defi\
  79.      ne FO\
  80.      O 10\
  81.      20
  82. is equivalent into `#define FOO 1020'.  You can split even an escape
  83. sequence with Backslash-Newline.  For example, you can split `"foo\bar"'
  84. between the `\' and the `b' to get
  85.      "foo\\
  86.      bar"
  87. This behavior is unclean: in all other contexts, a Backslash can be
  88. inserted in a string constant as an ordinary character by writing a
  89. double Backslash, and this creates an exception.  But the ANSI C
  90. standard requires it.  (Strict ANSI C does not allow Newlines in string
  91. constants, so they do not consider this a problem.)
  92.    But there are a few exceptions to all three transformations.
  93.    * C comments and predefined macro names are not recognized inside a
  94.      `#include' command in which the file name is delimited with `<'
  95.      and `>'.
  96.    * C comments and predefined macro names are never recognized within a
  97.      character or string constant.  (Strictly speaking, this is the
  98.      rule, not an exception, but it is worth noting here anyway.)
  99.    * Backslash-Newline may not safely be used within an ANSI "trigraph".
  100.      Trigraphs are converted before Backslash-Newline is deleted.  If
  101.      you write what looks like a trigraph with a Backslash-Newline
  102.      inside, the Backslash-Newline is deleted as usual, but it is then
  103.      too late to recognize the trigraph.
  104.      This exception is relevant only if you use the `-trigraphs' option
  105.      to enable trigraph processing.  *Note Invocation::.
  106. File: cpp.info,  Node: Commands,  Next: Header Files,  Prev: Global Actions,  Up: Top
  107. Preprocessor Commands
  108. =====================
  109.    Most preprocessor features are active only if you use preprocessor
  110. commands to request their use.
  111.    Preprocessor commands are lines in your program that start with `#'.
  112. The `#' is followed by an identifier that is the "command name".  For
  113. example, `#define' is the command that defines a macro.  Whitespace is
  114. also allowed before and after the `#'.
  115.    The set of valid command names is fixed.  Programs cannot define new
  116. preprocessor commands.
  117.    Some command names require arguments; these make up the rest of the
  118. command line and must be separated from the command name by whitespace.
  119. For example, `#define' must be followed by a macro name and the
  120. intended expansion of the macro.  *Note Simple Macros::.
  121.    A preprocessor command cannot be more than one line in normal
  122. circumstances.  It may be split cosmetically with Backslash-Newline,
  123. but that has no effect on its meaning.  Comments containing Newlines
  124. can also divide the command into multiple lines, but the comments are
  125. changed to Spaces before the command is interpreted.  The only way a
  126. significant Newline can occur in a preprocessor command is within a
  127. string constant or character constant.  Note that most C compilers that
  128. might be applied to the output from the preprocessor do not accept
  129. string or character constants containing Newlines.
  130.    The `#' and the command name cannot come from a macro expansion.  For
  131. example, if `foo' is defined as a macro expanding to `define', that
  132. does not make `#foo' a valid preprocessor command.
  133. File: cpp.info,  Node: Header Files,  Next: Macros,  Prev: Commands,  Up: Top
  134. Header Files
  135. ============
  136.    A header file is a file containing C declarations and macro
  137. definitions (*note Macros::.) to be shared between several source
  138. files.  You request the use of a header file in your program with the C
  139. preprocessor command `#include'.
  140. * Menu:
  141. * Header Uses::         What header files are used for.
  142. * Include Syntax::      How to write `#include' commands.
  143. * Include Operation::   What `#include' does.
  144. * Once-Only::        Preventing multiple inclusion of one header file.
  145. * Inheritance::         Including one header file in another header file.
  146. File: cpp.info,  Node: Header Uses,  Next: Include Syntax,  Prev: Header Files,  Up: Header Files
  147. Uses of Header Files
  148. --------------------
  149.    Header files serve two kinds of purposes.
  150.    * System header files declare the interfaces to parts of the
  151.      operating system.  You include them in your program to supply the
  152.      definitions and declarations you need to invoke system calls and
  153.      libraries.
  154.    * Your own header files contain declarations for interfaces between
  155.      the source files of your program.  Each time you have a group of
  156.      related declarations and macro definitions all or most of which
  157.      are needed in several different source files, it is a good idea to
  158.      create a header file for them.
  159.    Including a header file produces the same results in C compilation as
  160. copying the header file into each source file that needs it.  But such
  161. copying would be time-consuming and error-prone.  With a header file,
  162. the related declarations appear in only one place.  If they need to be
  163. changed, they can be changed in one place, and programs that include
  164. the header file will automatically use the new version when next
  165. recompiled.  The header file eliminates the labor of finding and
  166. changing all the copies as well as the risk that a failure to find one
  167. copy will result in inconsistencies within a program.
  168.    The usual convention is to give header files names that end with
  169. `.h'.  Avoid unusual characters in header file names, as they reduce
  170. portability.
  171. File: cpp.info,  Node: Include Syntax,  Next: Include Operation,  Prev: Header Uses,  Up: Header Files
  172. The `#include' Command
  173. ----------------------
  174.    Both user and system header files are included using the preprocessor
  175. command `#include'.  It has three variants:
  176. `#include <FILE>'
  177.      This variant is used for system header files.  It searches for a
  178.      file named FILE in a list of directories specified by you, then in
  179.      a standard list of system directories.  You specify directories to
  180.      search for header files with the command option `-I' (*note
  181.      Invocation::.).  The option `-nostdinc' inhibits searching the
  182.      standard system directories; in this case only the directories you
  183.      specify are searched.
  184.      The parsing of this form of `#include' is slightly special because
  185.      comments are not recognized within the `<...>'.  Thus, in
  186.      `#include <x/*y>' the `/*' does not start a comment and the
  187.      command specifies inclusion of a system header file named `x/*y'.
  188.      Of course, a header file with such a name is unlikely to exist on
  189.      Unix, where shell wildcard features would make it hard to
  190.      manipulate.
  191.      The argument FILE may not contain a `>' character.  It may,
  192.      however, contain a `<' character.
  193. `#include "FILE"'
  194.      This variant is used for header files of your own program.  It
  195.      searches for a file named FILE first in the current directory,
  196.      then in the same directories used for system header files.  The
  197.      current directory is the directory of the current input file.  It
  198.      is tried first because it is presumed to be the location of the
  199.      files that the current input file refers to.  (If the `-I-' option
  200.      is used, the special treatment of the current directory is
  201.      inhibited.)
  202.      The argument FILE may not contain `"' characters.  If backslashes
  203.      occur within FILE, they are considered ordinary text characters,
  204.      not escape characters.  None of the character escape sequences
  205.      appropriate to string constants in C are processed.  Thus,
  206.      `#include "x\n\\y"' specifies a filename containing three
  207.      backslashes.  It is not clear why this behavior is ever useful, but
  208.      the ANSI standard specifies it.
  209. `#include ANYTHING ELSE'
  210.      This variant is called a "computed #include".  Any `#include'
  211.      command whose argument does not fit the above two forms is a
  212.      computed include.  The text ANYTHING ELSE is checked for macro
  213.      calls, which are expanded (*note Macros::.).  When this is done,
  214.      the result must fit one of the above two variants--in particular,
  215.      the expanded text must in the end be surrounded by either quotes
  216.      or angle braces.
  217.      This feature allows you to define a macro which controls the file
  218.      name to be used at a later point in the program.  One application
  219.      of this is to allow a site-specific configuration file for your
  220.      program to specify the names of the system include files to be
  221.      used.  This can help in porting the program to various operating
  222.      systems in which the necessary system header files are found in
  223.      different places.
  224. File: cpp.info,  Node: Include Operation,  Next: Once-Only,  Prev: Include Syntax,  Up: Header Files
  225. How `#include' Works
  226. --------------------
  227.    The `#include' command works by directing the C preprocessor to scan
  228. the specified file as input before continuing with the rest of the
  229. current file.  The output from the preprocessor contains the output
  230. already generated, followed by the output resulting from the included
  231. file, followed by the output that comes from the text after the
  232. `#include' command.  For example, given a header file `header.h' as
  233. follows,
  234.      char *test ();
  235. and a main program called `program.c' that uses the header file, like
  236. this,
  237.      int x;
  238.      #include "header.h"
  239.      
  240.      main ()
  241.      {
  242.        printf (test ());
  243.      }
  244. the output generated by the C preprocessor for `program.c' as input
  245. would be
  246.      int x;
  247.      char *test ();
  248.      
  249.      main ()
  250.      {
  251.        printf (test ());
  252.      }
  253.    Included files are not limited to declarations and macro
  254. definitions; those are merely the typical uses.  Any fragment of a C
  255. program can be included from another file.  The include file could even
  256. contain the beginning of a statement that is concluded in the
  257. containing file, or the end of a statement that was started in the
  258. including file.  However, a comment or a string or character constant
  259. may not start in the included file and finish in the including file.
  260. An unterminated comment, string constant or character constant in an
  261. included file is considered to end (with an error message) at the end
  262. of the file.
  263.    It is possible for a header file to begin or end a syntactic unit
  264. such as a function definition, but that would be very confusing, so
  265. don't do it.
  266.    The line following the `#include' command is always treated as a
  267. separate line by the C preprocessor even if the included file lacks a
  268. final newline.
  269. File: cpp.info,  Node: Once-Only,  Next: Inheritance,  Prev: Include Operation,  Up: Header Files
  270. Once-Only Include Files
  271. -----------------------
  272.    Very often, one header file includes another.  It can easily result
  273. that a certain header file is included more than once.  This may lead
  274. to errors, if the header file defines structure types or typedefs, and
  275. is certainly wasteful.  Therefore, we often wish to prevent multiple
  276. inclusion of a header file.
  277.    The standard way to do this is to enclose the entire real contents
  278. of the file in a conditional, like this:
  279.      #ifndef FILE_FOO_SEEN
  280.      #define FILE_FOO_SEEN
  281.      
  282.      THE ENTIRE FILE
  283.      
  284.      #endif /* FILE_FOO_SEEN */
  285.    The macro `FILE_FOO_SEEN' indicates that the file has been included
  286. once already.  In a user header file, the macro name should not begin
  287. with `_'.  In a system header file, this name should begin with `__' to
  288. avoid conflicts with user programs.  In any kind of header file, the
  289. macro name should contain the name of the file and some additional
  290. text, to avoid conflicts with other header files.
  291.    The GNU C preprocessor is programmed to notice when a header file
  292. uses this particular construct and handle it efficiently.  If a header
  293. file is contained entirely in a `#ifndef' conditional, then it records
  294. that fact.  If a subsequent `#include' specifies the same file, and the
  295. macro in the `#ifndef' is already defined, then the file is entirely
  296. skipped, without even reading it.
  297.    There is also an explicit command to tell the preprocessor that it
  298. need not include a file more than once.  This is called `#pragma once',
  299. and was used *in addition to* the `#ifndef' conditional around the
  300. contents of the header file.  `#pragma once' is now obsolete and should
  301. not be used at all.
  302.    In the Objective C language, there is a variant of `#include' called
  303. `#import' which includes a file, but does so at most once.  If you use
  304. `#import' *instead of* `#include', then you don't need the conditionals
  305. inside the header file to prevent multiple execution of the contents.
  306.    `#import' is obsolete because it is not a well designed feature.  It
  307. requires the users of a header file--the applications programmers--to
  308. know that a certain header file should only be included once.  It is
  309. much better for the header file's implementor to write the file so that
  310. users don't need to know this.  Using `#ifndef' accomplishes this goal.
  311. File: cpp.info,  Node: Inheritance,  Prev: Once-Only,  Up: Header Files
  312. Inheritance and Header Files
  313. ----------------------------
  314.    "Inheritance" is what happens when one object or file derives some
  315. of its contents by virtual copying from another object or file.  In the
  316. case of C header files, inheritance means that one header file includes
  317. another header file and then replaces or adds something.
  318.    If the inheriting header file and the base header file have different
  319. names, then inheritance is straightforward: simply write `#include
  320. "BASE"' in the inheriting file.
  321.    Sometimes it is necessary to give the inheriting file the same name
  322. as the base file.  This is less straightforward.
  323.    For example, suppose an application program uses the system header
  324. file `sys/signal.h', but the version of `/usr/include/sys/signal.h' on
  325. a particular system doesn't do what the application program expects.
  326. It might be convenient to define a "local" version, perhaps under the
  327. name `/usr/local/include/sys/signal.h', to override or add to the one
  328. supplied by the system.
  329.    You can do this by using the option `-I.' for compilation, and
  330. writing a file `sys/signal.h' that does what the application program
  331. expects.  But making this file include the standard `sys/signal.h' is
  332. not so easy--writing `#include <sys/signal.h>' in that file doesn't
  333. work, because it includes your own version of the file, not the
  334. standard system version.  Used in that file itself, this leads to an
  335. infinite recursion and a fatal error in compilation.
  336.    `#include </usr/include/sys/signal.h>' would find the proper file,
  337. but that is not clean, since it makes an assumption about where the
  338. system header file is found.  This is bad for maintenance, since it
  339. means that any change in where the system's header files are kept
  340. requires a change somewhere else.
  341.    The clean way to solve this problem is to use `#include_next', which
  342. means, "Include the *next* file with this name."  This command works
  343. like `#include' except in searching for the specified file: it starts
  344. searching the list of header file directories *after* the directory in
  345. which the current file was found.
  346.    Suppose you specify `-I /usr/local/include', and the list of
  347. directories to search also includes `/usr/include'; and suppose that
  348. both directories contain a file named `sys/signal.h'.  Ordinary
  349. `#include <sys/signal.h>' finds the file under `/usr/local/include'.
  350. If that file contains `#include_next <sys/signal.h>', it starts
  351. searching after that directory, and finds the file in `/usr/include'.
  352. File: cpp.info,  Node: Macros,  Next: Conditionals,  Prev: Header Files,  Up: Top
  353. Macros
  354. ======
  355.    A macro is a sort of abbreviation which you can define once and then
  356. use later.  There are many complicated features associated with macros
  357. in the C preprocessor.
  358. * Menu:
  359. * Simple Macros::    Macros that always expand the same way.
  360. * Argument Macros::  Macros that accept arguments that are substituted
  361.                        into the macro expansion.
  362. * Predefined::       Predefined macros that are always available.
  363. * Stringification::  Macro arguments converted into string constants.
  364. * Concatenation::    Building tokens from parts taken from macro arguments.
  365. * Undefining::       Cancelling a macro's definition.
  366. * Redefining::       Changing a macro's definition.
  367. * Macro Pitfalls::   Macros can confuse the unwary.  Here we explain
  368.                        several common problems and strange features.
  369. File: cpp.info,  Node: Simple Macros,  Next: Argument Macros,  Prev: Macros,  Up: Macros
  370. Simple Macros
  371. -------------
  372.    A "simple macro" is a kind of abbreviation.  It is a name which
  373. stands for a fragment of code.  Some people refer to these as "manifest
  374. constants".
  375.    Before you can use a macro, you must "define" it explicitly with the
  376. `#define' command.  `#define' is followed by the name of the macro and
  377. then the code it should be an abbreviation for.  For example,
  378.      #define BUFFER_SIZE 1020
  379. defines a macro named `BUFFER_SIZE' as an abbreviation for the text
  380. `1020'.  If somewhere after this `#define' command there comes a C
  381. statement of the form
  382.      foo = (char *) xmalloc (BUFFER_SIZE);
  383. then the C preprocessor will recognize and "expand" the macro
  384. `BUFFER_SIZE', resulting in
  385.      foo = (char *) xmalloc (1020);
  386.    The use of all upper case for macro names is a standard convention.
  387. Programs are easier to read when it is possible to tell at a glance
  388. which names are macros.
  389.    Normally, a macro definition must be a single line, like all C
  390. preprocessor commands.  (You can split a long macro definition
  391. cosmetically with Backslash-Newline.)  There is one exception: Newlines
  392. can be included in the macro definition if within a string or character
  393. constant.  This is because it is not possible for a macro definition to
  394. contain an unbalanced quote character; the definition automatically
  395. extends to include the matching quote character that ends the string or
  396. character constant.  Comments within a macro definition may contain
  397. Newlines, which make no difference since the comments are entirely
  398. replaced with Spaces regardless of their contents.
  399.    Aside from the above, there is no restriction on what can go in a
  400. macro body.  Parentheses need not balance.  The body need not resemble
  401. valid C code.  (But if it does not, you may get error messages from the
  402. C compiler when you use the macro.)
  403.    The C preprocessor scans your program sequentially, so macro
  404. definitions take effect at the place you write them.  Therefore, the
  405. following input to the C preprocessor
  406.      foo = X;
  407.      #define X 4
  408.      bar = X;
  409. produces as output
  410.      foo = X;
  411.      
  412.      bar = 4;
  413.    After the preprocessor expands a macro name, the macro's definition
  414. body is appended to the front of the remaining input, and the check for
  415. macro calls continues.  Therefore, the macro body can contain calls to
  416. other macros.  For example, after
  417.      #define BUFSIZE 1020
  418.      #define TABLESIZE BUFSIZE
  419. the name `TABLESIZE' when used in the program would go through two
  420. stages of expansion, resulting ultimately in `1020'.
  421.    This is not at all the same as defining `TABLESIZE' to be `1020'.
  422. The `#define' for `TABLESIZE' uses exactly the body you specify--in
  423. this case, `BUFSIZE'--and does not check to see whether it too is the
  424. name of a macro.  It's only when you *use* `TABLESIZE' that the result
  425. of its expansion is checked for more macro names.  *Note Cascaded
  426. Macros::.
  427. File: cpp.info,  Node: Argument Macros,  Next: Predefined,  Prev: Simple Macros,  Up: Macros
  428. Macros with Arguments
  429. ---------------------
  430.    A simple macro always stands for exactly the same text, each time it
  431. is used.  Macros can be more flexible when they accept "arguments".
  432. Arguments are fragments of code that you supply each time the macro is
  433. used.  These fragments are included in the expansion of the macro
  434. according to the directions in the macro definition.  A macro that
  435. accepts arguments is called a "function-like macro" because the syntax
  436. for using it looks like a function call.
  437.    To define a macro that uses arguments, you write a `#define' command
  438. with a list of "argument names" in parentheses after the name of the
  439. macro.  The argument names may be any valid C identifiers, separated by
  440. commas and optionally whitespace.  The open-parenthesis must follow the
  441. macro name immediately, with no space in between.
  442.    For example, here is a macro that computes the minimum of two numeric
  443. values, as it is defined in many C programs:
  444.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  445. (This is not the best way to define a "minimum" macro in GNU C.  *Note
  446. Side Effects::, for more information.)
  447.    To use a macro that expects arguments, you write the name of the
  448. macro followed by a list of "actual arguments" in parentheses,
  449. separated by commas.  The number of actual arguments you give must
  450. match the number of arguments the macro expects.   Examples of use of
  451. the macro `min' include `min (1, 2)' and `min (x + 28, *p)'.
  452.    The expansion text of the macro depends on the arguments you use.
  453. Each of the argument names of the macro is replaced, throughout the
  454. macro definition, with the corresponding actual argument.  Using the
  455. same macro `min' defined above, `min (1, 2)' expands into
  456.      ((1) < (2) ? (1) : (2))
  457. where `1' has been substituted for `X' and `2' for `Y'.
  458.    Likewise, `min (x + 28, *p)' expands into
  459.      ((x + 28) < (*p) ? (x + 28) : (*p))
  460.    Parentheses in the actual arguments must balance; a comma within
  461. parentheses does not end an argument.  However, there is no requirement
  462. for brackets or braces to balance, and they do not prevent a comma from
  463. separating arguments.  Thus,
  464.      macro (array[x = y, x + 1])
  465. passes two arguments to `macro': `array[x = y' and `x + 1]'.  If you
  466. want to supply `array[x = y, x + 1]' as an argument, you must write it
  467. as `array[(x = y, x + 1)]', which is equivalent C code.
  468.    After the actual arguments are substituted into the macro body, the
  469. entire result is appended to the front of the remaining input, and the
  470. check for macro calls continues.  Therefore, the actual arguments can
  471. contain calls to other macros, either with or without arguments, or
  472. even to the same macro.  The macro body can also contain calls to other
  473. macros.  For example, `min (min (a, b), c)' expands into this text:
  474.      ((((a) < (b) ? (a) : (b))) < (c)
  475.       ? (((a) < (b) ? (a) : (b)))
  476.       : (c))
  477. (Line breaks shown here for clarity would not actually be generated.)
  478.    If a macro `foo' takes one argument, and you want to supply an empty
  479. argument, you must write at least some whitespace between the
  480. parentheses, like this: `foo ( )'.  Just `foo ()' is providing no
  481. arguments, which is an error if `foo' expects an argument.  But `foo0
  482. ()' is the correct way to call a macro defined to take zero arguments,
  483. like this:
  484.      #define foo0() ...
  485.    If you use the macro name followed by something other than an
  486. open-parenthesis (after ignoring any spaces, tabs and comments that
  487. follow), it is not a call to the macro, and the preprocessor does not
  488. change what you have written.  Therefore, it is possible for the same
  489. name to be a variable or function in your program as well as a macro,
  490. and you can choose in each instance whether to refer to the macro (if
  491. an actual argument list follows) or the variable or function (if an
  492. argument list does not follow).
  493.    Such dual use of one name could be confusing and should be avoided
  494. except when the two meanings are effectively synonymous: that is, when
  495. the name is both a macro and a function and the two have similar
  496. effects.  You can think of the name simply as a function; use of the
  497. name for purposes other than calling it (such as, to take the address)
  498. will refer to the function, while calls will expand the macro and
  499. generate better but equivalent code.  For example, you can use a
  500. function named `min' in the same source file that defines the macro.
  501. If you write `&min' with no argument list, you refer to the function.
  502. If you write `min (x, bb)', with an argument list, the macro is
  503. expanded.  If you write `(min) (a, bb)', where the name `min' is not
  504. followed by an open-parenthesis, the macro is not expanded, so you wind
  505. up with a call to the function `min'.
  506.    You may not define the same name as both a simple macro and a macro
  507. with arguments.
  508.    In the definition of a macro with arguments, the list of argument
  509. names must follow the macro name immediately with no space in between.
  510. If there is a space after the macro name, the macro is defined as
  511. taking no arguments, and all the rest of the line is taken to be the
  512. expansion.  The reason for this is that it is often useful to define a
  513. macro that takes no arguments and whose definition begins with an
  514. identifier in parentheses.  This rule about spaces makes it possible
  515. for you to do either this:
  516.      #define FOO(x) - 1 / (x)
  517. (which defines `FOO' to take an argument and expand into minus the
  518. reciprocal of that argument) or this:
  519.      #define BAR (x) - 1 / (x)
  520. (which defines `BAR' to take no argument and always expand into `(x) -
  521. 1 / (x)').
  522.    Note that the *uses* of a macro with arguments can have spaces before
  523. the left parenthesis; it's the *definition* where it matters whether
  524. there is a space.
  525. File: cpp.info,  Node: Predefined,  Next: Stringification,  Prev: Argument Macros,  Up: Macros
  526. Predefined Macros
  527. -----------------
  528.    Several simple macros are predefined.  You can use them without
  529. giving definitions for them.  They fall into two classes: standard
  530. macros and system-specific macros.
  531. * Menu:
  532. * Standard Predefined::     Standard predefined macros.
  533. * Nonstandard Predefined::  Nonstandard predefined macros.
  534. File: cpp.info,  Node: Standard Predefined,  Next: Nonstandard Predefined,  Prev: Predefined,  Up: Predefined
  535. Standard Predefined Macros
  536. ..........................
  537.    The standard predefined macros are available with the same meanings
  538. regardless of the machine or operating system on which you are using
  539. GNU C.  Their names all start and end with double underscores.  Those
  540. preceding `__GNUC__' in this table are standardized by ANSI C; the rest
  541. are GNU C extensions.
  542. `__FILE__'
  543.      This macro expands to the name of the current input file, in the
  544.      form of a C string constant.  The precise name returned is the one
  545.      that was specified in `#include' or as the input file name
  546.      argument.
  547. `__LINE__'
  548.      This macro expands to the current input line number, in the form
  549.      of a decimal integer constant.  While we call it a predefined
  550.      macro, it's a pretty strange macro, since its "definition" changes
  551.      with each new line of source code.
  552.      This and `__FILE__' are useful in generating an error message to
  553.      report an inconsistency detected by the program; the message can
  554.      state the source line at which the inconsistency was detected.
  555.      For example,
  556.           fprintf (stderr, "Internal error: "
  557.                            "negative string length "
  558.                            "%d at %s, line %d.",
  559.                    length, __FILE__, __LINE__);
  560.      A `#include' command changes the expansions of `__FILE__' and
  561.      `__LINE__' to correspond to the included file.  At the end of that
  562.      file, when processing resumes on the input file that contained the
  563.      `#include' command, the expansions of `__FILE__' and `__LINE__'
  564.      revert to the values they had before the `#include' (but
  565.      `__LINE__' is then incremented by one as processing moves to the
  566.      line after the `#include').
  567.      The expansions of both `__FILE__' and `__LINE__' are altered if a
  568.      `#line' command is used.  *Note Combining Sources::.
  569. `__INCLUDE_LEVEL__'
  570.      This macro expands to a decimal integer constant that represents
  571.      the depth of nesting in include files.  The value of this macro is
  572.      incremented on every `#include' command and decremented at every
  573.      end of file.  For input files specified by command line arguments,
  574.      the nesting level is zero.
  575. `__DATE__'
  576.      This macro expands to a string constant that describes the date on
  577.      which the preprocessor is being run.  The string constant contains
  578.      eleven characters and looks like `"Jan 29 1987"' or `"Apr 1 1905"'.
  579. `__TIME__'
  580.      This macro expands to a string constant that describes the time at
  581.      which the preprocessor is being run.  The string constant contains
  582.      eight characters and looks like `"23:59:01"'.
  583. `__STDC__'
  584.      This macro expands to the constant 1, to signify that this is ANSI
  585.      Standard C.  (Whether that is actually true depends on what C
  586.      compiler will operate on the output from the preprocessor.)
  587. `__GNUC__'
  588.      This macro is defined if and only if this is GNU C.  This macro is
  589.      defined only when the entire GNU C compiler is in use; if you
  590.      invoke the preprocessor directly, `__GNUC__' is undefined.  The
  591.      value identifies the major version number of GNU CC (`1' for GNU CC
  592.      version 1, which is now obsolete, and `2' for version 2).
  593. `__GNUG__'
  594.      The GNU C compiler defines this when the compilation language is
  595.      C++; use `__GNUG__' to distinguish between GNU C and GNU C++.
  596. `__cplusplus'
  597.      The draft ANSI standard for C++ used to require predefining this
  598.      variable.  Though it is no longer required, GNU C++ continues to
  599.      define it, as do other popular C++ compilers.  You can use
  600.      `__cplusplus' to test whether a header is compiled by a C compiler
  601.      or a C++ compiler.
  602. `__STRICT_ANSI__'
  603.      This macro is defined if and only if the `-ansi' switch was
  604.      specified when GNU C was invoked.  Its definition is the null
  605.      string.  This macro exists primarily to direct certain GNU header
  606.      files not to define certain traditional Unix constructs which are
  607.      incompatible with ANSI C.
  608. `__BASE_FILE__'
  609.      This macro expands to the name of the main input file, in the form
  610.      of a C string constant.  This is the source file that was specified
  611.      as an argument when the C compiler was invoked.
  612. `__VERSION__'
  613.      This macro expands to a string which describes the version number
  614.      of GNU C.  The string is normally a sequence of decimal numbers
  615.      separated by periods, such as `"2.6.0"'.  The only reasonable use
  616.      of this macro is to incorporate it into a string constant.
  617. `__OPTIMIZE__'
  618.      This macro is defined in optimizing compilations.  It causes
  619.      certain GNU header files to define alternative macro definitions
  620.      for some system library functions.  It is unwise to refer to or
  621.      test the definition of this macro unless you make very sure that
  622.      programs will execute with the same effect regardless.
  623. `__CHAR_UNSIGNED__'
  624.      This macro is defined if and only if the data type `char' is
  625.      unsigned on the target machine.  It exists to cause the standard
  626.      header file `limit.h' to work correctly.  It is bad practice to
  627.      refer to this macro yourself; instead, refer to the standard
  628.      macros defined in `limit.h'.  The preprocessor uses this macro to
  629.      determine whether or not to sign-extend large character constants
  630.      written in octal; see *Note The `#if' Command: #if Command.
  631. File: cpp.info,  Node: Nonstandard Predefined,  Prev: Standard Predefined,  Up: Predefined
  632. Nonstandard Predefined Macros
  633. .............................
  634.    The C preprocessor normally has several predefined macros that vary
  635. between machines because their purpose is to indicate what type of
  636. system and machine is in use.  This manual, being for all systems and
  637. machines, cannot tell you exactly what their names are; instead, we
  638. offer a list of some typical ones.  You can use `cpp -dM' to see the
  639. values of predefined macros; see *Note Invocation::.
  640.    Some nonstandard predefined macros describe the operating system in
  641. use, with more or less specificity.  For example,
  642. `unix'
  643.      `unix' is normally predefined on all Unix systems.
  644. `BSD'
  645.      `BSD' is predefined on recent versions of Berkeley Unix (perhaps
  646.      only in version 4.3).
  647.    Other nonstandard predefined macros describe the kind of CPU, with
  648. more or less specificity.  For example,
  649. `vax'
  650.      `vax' is predefined on Vax computers.
  651. `mc68000'
  652.      `mc68000' is predefined on most computers whose CPU is a Motorola
  653.      68000, 68010 or 68020.
  654. `m68k'
  655.      `m68k' is also predefined on most computers whose CPU is a 68000,
  656.      68010 or 68020; however, some makers use `mc68000' and some use
  657.      `m68k'.  Some predefine both names.  What happens in GNU C depends
  658.      on the system you are using it on.
  659. `M68020'
  660.      `M68020' has been observed to be predefined on some systems that
  661.      use 68020 CPUs--in addition to `mc68000' and `m68k', which are
  662.      less specific.
  663. `_AM29K'
  664. `_AM29000'
  665.      Both `_AM29K' and `_AM29000' are predefined for the AMD 29000 CPU
  666.      family.
  667. `ns32000'
  668.      `ns32000' is predefined on computers which use the National
  669.      Semiconductor 32000 series CPU.
  670.    Yet other nonstandard predefined macros describe the manufacturer of
  671. the system.  For example,
  672. `sun'
  673.      `sun' is predefined on all models of Sun computers.
  674. `pyr'
  675.      `pyr' is predefined on all models of Pyramid computers.
  676. `sequent'
  677.      `sequent' is predefined on all models of Sequent computers.
  678.    These predefined symbols are not only nonstandard, they are contrary
  679. to the ANSI standard because their names do not start with underscores.
  680. Therefore, the option `-ansi' inhibits the definition of these symbols.
  681.    This tends to make `-ansi' useless, since many programs depend on the
  682. customary nonstandard predefined symbols.  Even system header files
  683. check them and will generate incorrect declarations if they do not find
  684. the names that are expected.  You might think that the header files
  685. supplied for the Uglix computer would not need to test what machine
  686. they are running on, because they can simply assume it is the Uglix;
  687. but often they do, and they do so using the customary names.  As a
  688. result, very few C programs will compile with `-ansi'.  We intend to
  689. avoid such problems on the GNU system.
  690.    What, then, should you do in an ANSI C program to test the type of
  691. machine it will run on?
  692.    GNU C offers a parallel series of symbols for this purpose, whose
  693. names are made from the customary ones by adding `__' at the beginning
  694. and end.  Thus, the symbol `__vax__' would be available on a Vax, and
  695. so on.
  696.    The set of nonstandard predefined names in the GNU C preprocessor is
  697. controlled (when `cpp' is itself compiled) by the macro
  698. `CPP_PREDEFINES', which should be a string containing `-D' options,
  699. separated by spaces.  For example, on the Sun 3, we use the following
  700. definition:
  701.      #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
  702. This macro is usually specified in `tm.h'.
  703. File: cpp.info,  Node: Stringification,  Next: Concatenation,  Prev: Predefined,  Up: Macros
  704. Stringification
  705. ---------------
  706.    "Stringification" means turning a code fragment into a string
  707. constant whose contents are the text for the code fragment.  For
  708. example, stringifying `foo (z)' results in `"foo (z)"'.
  709.    In the C preprocessor, stringification is an option available when
  710. macro arguments are substituted into the macro definition.  In the body
  711. of the definition, when an argument name appears, the character `#'
  712. before the name specifies stringification of the corresponding actual
  713. argument when it is substituted at that point in the definition.  The
  714. same argument may be substituted in other places in the definition
  715. without stringification if the argument name appears in those places
  716. with no `#'.
  717.    Here is an example of a macro definition that uses stringification:
  718.      #define WARN_IF(EXP) \
  719.      do { if (EXP) \
  720.              fprintf (stderr, "Warning: " #EXP "\n"); } \
  721.      while (0)
  722. Here the actual argument for `EXP' is substituted once as given, into
  723. the `if' statement, and once as stringified, into the argument to
  724. `fprintf'.  The `do' and `while (0)' are a kludge to make it possible
  725. to write `WARN_IF (ARG);', which the resemblance of `WARN_IF' to a
  726. function would make C programmers want to do; see *Note Swallow
  727. Semicolon::.
  728.    The stringification feature is limited to transforming one macro
  729. argument into one string constant: there is no way to combine the
  730. argument with other text and then stringify it all together.  But the
  731. example above shows how an equivalent result can be obtained in ANSI
  732. Standard C using the feature that adjacent string constants are
  733. concatenated as one string constant.  The preprocessor stringifies the
  734. actual value of `EXP' into a separate string constant, resulting in
  735. text like
  736.      do { if (x == 0) \
  737.              fprintf (stderr, "Warning: " "x == 0" "\n"); } \
  738.      while (0)
  739. but the C compiler then sees three consecutive string constants and
  740. concatenates them into one, producing effectively
  741.      do { if (x == 0) \
  742.              fprintf (stderr, "Warning: x == 0\n"); } \
  743.      while (0)
  744.    Stringification in C involves more than putting doublequote
  745. characters around the fragment; it is necessary to put backslashes in
  746. front of all doublequote characters, and all backslashes in string and
  747. character constants, in order to get a valid C string constant with the
  748. proper contents.  Thus, stringifying `p = "foo\n";' results in `"p =
  749. \"foo\\n\";"'.  However, backslashes that are not inside of string or
  750. character constants are not duplicated: `\n' by itself stringifies to
  751. `"\n"'.
  752.    Whitespace (including comments) in the text being stringified is
  753. handled according to precise rules.  All leading and trailing
  754. whitespace is ignored.  Any sequence of whitespace in the middle of the
  755. text is converted to a single space in the stringified result.
  756. File: cpp.info,  Node: Concatenation,  Next: Undefining,  Prev: Stringification,  Up: Macros
  757. Concatenation
  758. -------------
  759.    "Concatenation" means joining two strings into one.  In the context
  760. of macro expansion, concatenation refers to joining two lexical units
  761. into one longer one.  Specifically, an actual argument to the macro can
  762. be concatenated with another actual argument or with fixed text to
  763. produce a longer name.  The longer name might be the name of a function,
  764. variable or type, or a C keyword; it might even be the name of another
  765. macro, in which case it will be expanded.
  766.    When you define a macro, you request concatenation with the special
  767. operator `##' in the macro body.  When the macro is called, after
  768. actual arguments are substituted, all `##' operators are deleted, and
  769. so is any whitespace next to them (including whitespace that was part
  770. of an actual argument).  The result is to concatenate the syntactic
  771. tokens on either side of the `##'.
  772.    Consider a C program that interprets named commands.  There probably
  773. needs to be a table of commands, perhaps an array of structures
  774. declared as follows:
  775.      struct command
  776.      {
  777.        char *name;
  778.        void (*function) ();
  779.      };
  780.      
  781.      struct command commands[] =
  782.      {
  783.        { "quit", quit_command},
  784.        { "help", help_command},
  785.        ...
  786.      };
  787.    It would be cleaner not to have to give each command name twice,
  788. once in the string constant and once in the function name.  A macro
  789. which takes the name of a command as an argument can make this
  790. unnecessary.  The string constant can be created with stringification,
  791. and the function name by concatenating the argument with `_command'.
  792. Here is how it is done:
  793.      #define COMMAND(NAME)  { #NAME, NAME ## _command }
  794.      
  795.      struct command commands[] =
  796.      {
  797.        COMMAND (quit),
  798.        COMMAND (help),
  799.        ...
  800.      };
  801.    The usual case of concatenation is concatenating two names (or a
  802. name and a number) into a longer name.  But this isn't the only valid
  803. case.  It is also possible to concatenate two numbers (or a number and
  804. a name, such as `1.5' and `e3') into a number.  Also, multi-character
  805. operators such as `+=' can be formed by concatenation.  In some cases
  806. it is even possible to piece together a string constant.  However, two
  807. pieces of text that don't together form a valid lexical unit cannot be
  808. concatenated.  For example, concatenation with `x' on one side and `+'
  809. on the other is not meaningful because those two characters can't fit
  810. together in any lexical unit of C.  The ANSI standard says that such
  811. attempts at concatenation are undefined, but in the GNU C preprocessor
  812. it is well defined: it puts the `x' and `+' side by side with no
  813. particular special results.
  814.    Keep in mind that the C preprocessor converts comments to whitespace
  815. before macros are even considered.  Therefore, you cannot create a
  816. comment by concatenating `/' and `*': the `/*' sequence that starts a
  817. comment is not a lexical unit, but rather the beginning of a "long"
  818. space character.  Also, you can freely use comments next to a `##' in a
  819. macro definition, or in actual arguments that will be concatenated,
  820. because the comments will be converted to spaces at first sight, and
  821. concatenation will later discard the spaces.
  822. File: cpp.info,  Node: Undefining,  Next: Redefining,  Prev: Concatenation,  Up: Macros
  823. Undefining Macros
  824. -----------------
  825.    To "undefine" a macro means to cancel its definition.  This is done
  826. with the `#undef' command.  `#undef' is followed by the macro name to
  827. be undefined.
  828.    Like definition, undefinition occurs at a specific point in the
  829. source file, and it applies starting from that point.  The name ceases
  830. to be a macro name, and from that point on it is treated by the
  831. preprocessor as if it had never been a macro name.
  832.    For example,
  833.      #define FOO 4
  834.      x = FOO;
  835.      #undef FOO
  836.      x = FOO;
  837. expands into
  838.      x = 4;
  839.      
  840.      x = FOO;
  841. In this example, `FOO' had better be a variable or function as well as
  842. (temporarily) a macro, in order for the result of the expansion to be
  843. valid C code.
  844.    The same form of `#undef' command will cancel definitions with
  845. arguments or definitions that don't expect arguments.  The `#undef'
  846. command has no effect when used on a name not currently defined as a
  847. macro.
  848. File: cpp.info,  Node: Redefining,  Next: Macro Pitfalls,  Prev: Undefining,  Up: Macros
  849. Redefining Macros
  850. -----------------
  851.    "Redefining" a macro means defining (with `#define') a name that is
  852. already defined as a macro.
  853.    A redefinition is trivial if the new definition is transparently
  854. identical to the old one.  You probably wouldn't deliberately write a
  855. trivial redefinition, but they can happen automatically when a header
  856. file is included more than once (*note Header Files::.), so they are
  857. accepted silently and without effect.
  858.    Nontrivial redefinition is considered likely to be an error, so it
  859. provokes a warning message from the preprocessor.  However, sometimes it
  860. is useful to change the definition of a macro in mid-compilation.  You
  861. can inhibit the warning by undefining the macro with `#undef' before the
  862. second definition.
  863.    In order for a redefinition to be trivial, the new definition must
  864. exactly match the one already in effect, with two possible exceptions:
  865.    * Whitespace may be added or deleted at the beginning or the end.
  866.    * Whitespace may be changed in the middle (but not inside strings).
  867.      However, it may not be eliminated entirely, and it may not be added
  868.      where there was no whitespace at all.
  869.    Recall that a comment counts as whitespace.
  870. File: cpp.info,  Node: Macro Pitfalls,  Prev: Redefining,  Up: Macros
  871. Pitfalls and Subtleties of Macros
  872. ---------------------------------
  873.    In this section we describe some special rules that apply to macros
  874. and macro expansion, and point out certain cases in which the rules have
  875. counterintuitive consequences that you must watch out for.
  876. * Menu:
  877. * Misnesting::        Macros can contain unmatched parentheses.
  878. * Macro Parentheses:: Why apparently superfluous parentheses
  879.                          may be necessary to avoid incorrect grouping.
  880. * Swallow Semicolon:: Macros that look like functions
  881.                          but expand into compound statements.
  882. * Side Effects::      Unsafe macros that cause trouble when
  883.                          arguments contain side effects.
  884. * Self-Reference::    Macros whose definitions use the macros' own names.
  885. * Argument Prescan::  Actual arguments are checked for macro calls
  886.                          before they are substituted.
  887. * Cascaded Macros::   Macros whose definitions use other macros.
  888. * Newlines in Args::  Sometimes line numbers get confused.
  889. File: cpp.info,  Node: Misnesting,  Next: Macro Parentheses,  Prev: Macro Pitfalls,  Up: Macro Pitfalls
  890. Improperly Nested Constructs
  891. ............................
  892.    Recall that when a macro is called with arguments, the arguments are
  893. substituted into the macro body and the result is checked, together with
  894. the rest of the input file, for more macro calls.
  895.    It is possible to piece together a macro call coming partially from
  896. the macro body and partially from the actual arguments.  For example,
  897.      #define double(x) (2*(x))
  898.      #define call_with_1(x) x(1)
  899. would expand `call_with_1 (double)' into `(2*(1))'.
  900.    Macro definitions do not have to have balanced parentheses.  By
  901. writing an unbalanced open parenthesis in a macro body, it is possible
  902. to create a macro call that begins inside the macro body but ends
  903. outside of it.  For example,
  904.      #define strange(file) fprintf (file, "%s %d",
  905.      ...
  906.      strange(stderr) p, 35)
  907. This bizarre example expands to `fprintf (stderr, "%s %d", p, 35)'!
  908.